﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

namespace EHTWebsite.Code
{
    /// <summary>
    /// Author: toanvt
    /// CreatedDate: 12/12/2011
    /// Description: Liên Hệ provider
    /// </summary>
    public class LienHeProvider
    {
        /// <summary>
        /// Author: toanvt
        /// CreatedDate: 12/12/2011
        /// Description: Inser, Update Liên Hệ
        /// </summary>
        /// <param name="theLienHe">Đối tượng Liên Hệ</param>
        /// <param name="IsAdd">Trang thái thêm mới hay sửa</param>
        public static void IULienHe(LienHe theLienHe, bool IsAdd)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "proc_IULienHe";
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter("@LienHeID", theLienHe.LienHeID));
            command.Parameters.Add(new SqlParameter("@HoTen", theLienHe.HoTen));
            command.Parameters.Add(new SqlParameter("@DiaChi", theLienHe.DiaChi));
            command.Parameters.Add(new SqlParameter("@DienThoai", theLienHe.DienThoai));
            command.Parameters.Add(new SqlParameter("@Fax", theLienHe.Fax));
            command.Parameters.Add(new SqlParameter("@Email", theLienHe.Email));
            command.Parameters.Add(new SqlParameter("@NoiDung", theLienHe.NoiDung));
            command.Parameters.Add(new SqlParameter("@IsAdd", IsAdd));

            DataProvider.ExecuteNoneQuery(command);
        }
        

        /// <summary>
        /// Author: toanvt
        /// CreatedDate: 12/12/2011
        /// Description: Xóa Liên Hệ bởi Mã Liên Hệ
        /// </summary>
        /// <param name="LienHeID">Mã liên hệ</param>
        public static void DeleteLienHeByLienHeID(Int32 LienHeID)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "proc_DeleteLienHeByLienHeID";
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter("@LienHeID", LienHeID));

            DataProvider.ExecuteNoneQuery(command);
        }

        /// <summary>
        /// Author: toanvt
        /// CreatedDate: 12/12/2011
        /// Description: Lấy danh sách Liên Hệ có phân trang
        /// </summary>
        /// <param name="pageIndex">Trang hiện tại</param>
        /// <param name="pageSize">Số record trên 1 trang</param>
        /// <param name="totalRecord">Tổng số record</param>
        /// <returns>Danh sách liên hệ</returns>
        public static DataSet GetLienHes(Int32 pageIndex, Int32 pageSize, out Int32 totalRecord)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "proc_GetLienHes";
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter("@PageIndex", pageIndex));
            command.Parameters.Add(new SqlParameter("@PageSize", pageSize));

            return DataProvider.ExecuteDataSet(command, out totalRecord);
        }

        /// <summary>
        /// Author: toanvt
        /// CreatedDate: 12/12/2011
        /// Description: Lấy thông tin Liên Hệ theo Mã Liên Hệ
        /// </summary>
        /// <param name="LienHeID">Mã tin Liên Hệ</param>
        /// <returns>Đối tượng Liên Hệ</returns>
        public static LienHe GetLienHeByLienHeID(Int32 LienHeID)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "proc_GetLienHeByLienHeID";
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter("@LienHeID", LienHeID));

            DataSet ds = DataProvider.ExecuteDataSet(command);
            LienHe theLienHe = null;

            if (ds.Tables[0].Rows.Count > 0)
            {
                theLienHe = new LienHe();
                theLienHe.HoTen = ds.Tables[0].Rows[0]["HoTen"].ToString();
                theLienHe.DiaChi = ds.Tables[0].Rows[0]["DiaChi"].ToString();
                theLienHe.DienThoai = ds.Tables[0].Rows[0]["DienThoai"].ToString();
                theLienHe.Email = ds.Tables[0].Rows[0]["Email"].ToString();
                theLienHe.NoiDung = ds.Tables[0].Rows[0]["NoiDung"].ToString();
                theLienHe.Fax = ds.Tables[0].Rows[0]["Fax"].ToString();

                try
                {
                    theLienHe.NgayLienHe = DateTime.Parse(ds.Tables[0].Rows[0]["NgayLienHe"].ToString());
                }
                catch (Exception)
                {

                }
                theLienHe.TrangThaiDoc = Boolean.Parse(ds.Tables[0].Rows[0]["TrangThaiDoc"].ToString());
                theLienHe.LienHeID = Int32.Parse(ds.Tables[0].Rows[0]["LienHeID"].ToString());
            }

            return theLienHe;
        }
    }
}